package com.bittrex.bittrex.controller;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import de.elbatya.cryptocoins.bittrexclient.BittrexClient;
import de.elbatya.cryptocoins.bittrexclient.api.model.accountapi.Balance;
import de.elbatya.cryptocoins.bittrexclient.api.model.accountapi.DepositAddress;
import de.elbatya.cryptocoins.bittrexclient.api.model.accountapi.DepositHistoryEntry;
import de.elbatya.cryptocoins.bittrexclient.api.model.accountapi.Order;
import de.elbatya.cryptocoins.bittrexclient.api.model.accountapi.OrderHistoryEntry;
import de.elbatya.cryptocoins.bittrexclient.api.model.accountapi.WithdrawalHistoryEntry;
import de.elbatya.cryptocoins.bittrexclient.api.model.accountapi.WithdrawalRequested;
import de.elbatya.cryptocoins.bittrexclient.api.model.common.ApiResult;
import de.elbatya.cryptocoins.bittrexclient.config.ApiCredentials;

@RequestMapping("/account")
@RestController
public class BittrexAccountApiController
{

    @Value("${apikey}")
    private String key;

    @Value("${secret}")
    private String secret;

    /**
     * 
     * @创建人: Administrator
     * @方法描述:用于您的账户中检索所有余额
     * @创建时间: 2018年8月11日 上午11:43:41
     * @return ApiResult<List<Balance>>
     */
    @RequestMapping("/getbalances")
    public ApiResult<List<Balance>> getBalances()
    {
        // Ceate ApiCredentials with ApiKey and Secret from bittrex.com
        ApiCredentials credentials = new ApiCredentials(key, secret);

        // Create a BittrexClient with the ApiCredentials
        BittrexClient bittrexClient = new BittrexClient(credentials);

        // Perform a getOpenOrders request on the market api
        ApiResult<List<Balance>> apiResult = bittrexClient.getAccountApi().getBalances();
        // Unwrap the results
        // List<OpenOrder> openOrders = apiResult.unwrap();
        return apiResult;
    }

    /**
     * 
     * @创建人: Administrator
     * @方法描述: 用于从您的账户中检索特定货币的余额
     * @创建时间: 2018年8月11日 上午11:48:25
     * @param currency
     * @return ApiResult<Balance>
     */
    @RequestMapping("/getbalance")
    public ApiResult<Balance> getBalance(String currency)
    {
        // Ceate ApiCredentials with ApiKey and Secret from bittrex.com
        ApiCredentials credentials = new ApiCredentials(key, secret);

        // Create a BittrexClient with the ApiCredentials
        BittrexClient bittrexClient = new BittrexClient(credentials);

        // Perform a getOpenOrders request on the market api
        ApiResult<Balance> apiResult = bittrexClient.getAccountApi().getBalance(currency);
        // Unwrap the results
        // List<OpenOrder> openOrders = apiResult.unwrap();
        return apiResult;
    }

    /**
     * 
     * @创建人: Administrator
     * @方法描述: 用于检索或生成特定货币的地址。如果不存在，则调用失败返回ADDRESS_GENERATING,直到有一个可用
     * @创建时间: 2018年8月11日 上午11:50:32
     * @param currency
     * @return ApiResult<DepositAddress>
     */
    @RequestMapping("/getdepositaddress")
    public ApiResult<DepositAddress> getDepositAddress(String currency)
    {
        // Ceate ApiCredentials with ApiKey and Secret from bittrex.com
        ApiCredentials credentials = new ApiCredentials(key, secret);

        // Create a BittrexClient with the ApiCredentials
        BittrexClient bittrexClient = new BittrexClient(credentials);

        // Perform a getOpenOrders request on the market api
        ApiResult<DepositAddress> apiResult = bittrexClient.getAccountApi().getDepositAddress(currency);
        // Unwrap the results
        // List<OpenOrder> openOrders = apiResult.unwrap();
        return apiResult;
    }

    /**
     * 
     * @创建人: Administrator
     * @方法描述: 用于从您的账户中提取资金。注意：请说明txfee
     * @创建时间: 2018年8月11日 上午11:55:07
     * @param currency
     * @param quantity
     * @param address
     * @return ApiResult<WithdrawalRequested>
     */
    @RequestMapping("/withdraw")
    public ApiResult<WithdrawalRequested> getDepositAddress(String currency, BigDecimal quantity, String address)
    {
        // Ceate ApiCredentials with ApiKey and Secret from bittrex.com
        ApiCredentials credentials = new ApiCredentials(key, secret);

        // Create a BittrexClient with the ApiCredentials
        BittrexClient bittrexClient = new BittrexClient(credentials);

        // Perform a getOpenOrders request on the market api
        ApiResult<WithdrawalRequested> apiResult = bittrexClient.getAccountApi().withdraw(currency, quantity, address);
        // Unwrap the results
        // List<OpenOrder> openOrders = apiResult.unwrap();
        return apiResult;
    }

    /**
     * 
     * @创建人: Administrator
     * @方法描述: 用于通过uuid检索单个订单
     * @创建时间: 2018年8月11日 上午11:57:32
     * @param uuid
     * @return ApiResult<Order>
     */
    @RequestMapping("/getorder")
    public ApiResult<Order> getOrder(String uuid)
    {
        // Ceate ApiCredentials with ApiKey and Secret from bittrex.com
        ApiCredentials credentials = new ApiCredentials(key, secret);

        // Create a BittrexClient with the ApiCredentials
        BittrexClient bittrexClient = new BittrexClient(credentials);

        // Perform a getOpenOrders request on the market api
        ApiResult<Order> apiResult = bittrexClient.getAccountApi().getOrder(uuid);
        // Unwrap the results
        // List<OpenOrder> openOrders = apiResult.unwrap();
        return apiResult;
    }

    /**
     * 
     * @创建人: Administrator
     * @方法描述:用于检索您的订单历史记录 market可选
     * @创建时间: 2018年8月11日 上午11:59:50
     * @return ApiResult<List<OrderHistoryEntry>>
     */
    @RequestMapping("/getorderhistory")
    public ApiResult<List<OrderHistoryEntry>> getOrderHistory()
    {
        // Ceate ApiCredentials with ApiKey and Secret from bittrex.com
        ApiCredentials credentials = new ApiCredentials(key, secret);

        // Create a BittrexClient with the ApiCredentials
        BittrexClient bittrexClient = new BittrexClient(credentials);

        // Perform a getOpenOrders request on the market api
        ApiResult<List<OrderHistoryEntry>> apiResult = bittrexClient.getAccountApi().getOrderHistory();
        // Unwrap the results
        // List<OpenOrder> openOrders = apiResult.unwrap();
        return apiResult;
    }

    /**
     * 
     * @创建人: Administrator
     * @方法描述: 用于检索您的提款历史记录 currency可选
     * @创建时间: 2018年8月11日 下午12:02:22
     * @return ApiResult<List<WithdrawalHistoryEntry>>
     */
    @RequestMapping("/getwithdrawalhistory")
    public ApiResult<List<WithdrawalHistoryEntry>> getWithdrawalHistory()
    {
        // Ceate ApiCredentials with ApiKey and Secret from bittrex.com
        ApiCredentials credentials = new ApiCredentials(key, secret);

        // Create a BittrexClient with the ApiCredentials
        BittrexClient bittrexClient = new BittrexClient(credentials);

        // Perform a getOpenOrders request on the market api
        ApiResult<List<WithdrawalHistoryEntry>> apiResult = bittrexClient.getAccountApi().getWithdrawalHistory();
        // Unwrap the results
        // List<OpenOrder> openOrders = apiResult.unwrap();
        return apiResult;
    }

    /**
     * 
     * @创建人: Administrator
     * @方法描述: 用于检索您的存款历史记录
     * @创建时间: 2018年8月11日 下午12:05:20
     * @return ApiResult<List<DepositHistoryEntry>>
     */
    @RequestMapping("/getdeposithistory")
    public ApiResult<List<DepositHistoryEntry>> getDepositHistory()
    {
        // Ceate ApiCredentials with ApiKey and Secret from bittrex.com
        ApiCredentials credentials = new ApiCredentials(key, secret);

        // Create a BittrexClient with the ApiCredentials
        BittrexClient bittrexClient = new BittrexClient(credentials);

        // Perform a getOpenOrders request on the market api
        ApiResult<List<DepositHistoryEntry>> apiResult = bittrexClient.getAccountApi().getDepositHistory();
        // Unwrap the results
        // List<OpenOrder> openOrders = apiResult.unwrap();
        return apiResult;
    }
}
